-- lua stirng 扩展
-- Author: Colocasia
-- Date: 2019-07-03

local string = string
local table = table

local table_insert = table.insert;
local string_gsub = string.gsub;

---字符串分割
---@param s string
---@param delim string
---@return table | string
function string.split(s, delim)
    local split = {};
    local pattern = "[^".. delim .."]+";
    string_gsub(s, pattern, function(v) table_insert(split, v) end);
    return #split > 1 and split or s;
end

function string.ltrim(input)
    return string_gsub(input, "^[ \t\n\r]+", "")
end


function string.rtrim(input)
    return string_gsub(input, "[ \t\n\r]+$", "")
end


function string.trim(input)
    input = string_gsub(input, "^[ \t\n\r]+", "")
    return string_gsub(input, "[ \t\n\r]+$", "")
end

return string
